/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.dbobject.DbLogCleanRule;

import storage.util.StorageManager;

/**
 * 
 * @author fenyo rome
 *
 */
public class LogCleanRuleHelper {

	/**
	 * 将清洗规则保存到LaUDMS数据库中
	 * 
	 * @param cleanRule
	 *            要存储的模型对象
	 */
	public void saveCleanRule(DbLogCleanRule cleanRule) {
		// StorageManager sm = new StorageManager(StorageParameter.db_keySpace,
		// StorageParameter.db_columnFamily_cleanRule);
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_cleanRule"));
		// sm.open();
		sm.setRowKey(cleanRule.getRowKey());
		sm.put(DbLogCleanRule.CL_NAME, cleanRule.getName());
		sm.put(DbLogCleanRule.CL_CLEANRULE, cleanRule.getCleanRule());
		sm.put(DbLogCleanRule.CL_CREATETIME, cleanRule.getCreateTime());
		// sm.close();
	}

	/**
	 * 
	 * @param columnName
	 * columnName
	 * @param value
	 * value
	 * @return
	 */
	public List<DbLogCleanRule> getCleanRules(final String columnName,
			final String value) {
		List<DbLogCleanRule> result = new LinkedList<DbLogCleanRule>();
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_cleanRule"));
		// sm.open();
		List<KeySlice> crs = sm.get(columnName, value);
		try {
			for (KeySlice ks : crs) {
				String rowKey = new String(ks.getKey());
				String name = null, cleanRule = null, createTime = null;
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbLogCleanRule.CL_NAME)) {
						name = columnsValue;
					} else if (columnsName.equals(DbLogCleanRule.CL_CLEANRULE)) {
						cleanRule = columnsValue;
					} else if (columnsName.equals(DbLogCleanRule.CL_CREATETIME)) {
						createTime = columnsValue;
					}
				}
				DbLogCleanRule pLogCleanRule = new DbLogCleanRule(rowKey, name,
						cleanRule, createTime);
				result.add(pLogCleanRule);
			}
		} catch (Exception ex) {
			Logger.getLogger(LogCleanRuleHelper.class.getName()).log(
					Level.SEVERE, null, ex);
		} 
		return result;
	}
	/**
	 * 
	 * @return ArrayList<String>
	 */
	public ArrayList<String> getAllCleanRuleName() {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_cleanRule"));
		// sm.open();
		List<KeySlice> crs = sm.getAll();
		ArrayList<String> cleanRuleNameList = new ArrayList<String>();
		try {
			for (KeySlice ks : crs) {
				String name = "";
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn().getValue());
					if (columnsName.equals(DbLogCleanRule.CL_NAME)) {
						name = columnsValue;
					}
				}
				if (!cleanRuleNameList.contains(name) && name.length() > 0) {
					cleanRuleNameList.add(name);
				}
			}
		} catch (Exception ex) {
			Logger.getLogger(LogCleanRuleHelper.class.getName()).log(
					Level.SEVERE, null, ex);
		} 
		return cleanRuleNameList;
	}
	/**
	 * 
	 * @param rowKey
	 * rowKey
	 */
	public void deleteCleanRule(final String rowKey) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_cleanRule"));
		sm.delete(rowKey);
		// sm.close();
	}
}
